package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.appdetail.pojo.dto.SignInDetailDTO;
import com.xbongbong.pro.appdetail.pojo.vo.SignInDetailVO;
import com.xbongbong.pro.applist.pojo.dto.SignInTrackDTO;
import com.xbongbong.pro.applist.pojo.vo.SignInStatisticVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.weblist.pojo.dto.SignInDTO;
import com.xbongbong.pro.weblist.pojo.dto.SignInDeleteDTO;
import com.xbongbong.pro.weblist.pojo.dto.SignInSaveDTO;
import com.xbongbong.pro.weblist.pojo.dto.SignInStatisticDTO;
import com.xbongbong.pro.weblist.pojo.vo.SignInListVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.SignInService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/4/1 11:19
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/signIn")
public class MobileSignInController {

    @Resource
    private SignInService signInService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 签到统计
     * @param signInStatisticDto 签到统计入参
     * @return SignInStatisticsVO
     */
    @RequestMapping(value = "/statistic", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInStatistic(@RequestBody @Valid SignInStatisticDTO signInStatisticDto, BindingResult br) {
        XbbResponse<SignInStatisticVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.SIGNIN.getCode(), null, signInStatisticDto.getDistributorMark());
                SignInStatisticVO signInStatisticVO = signInService.signInStatistic(signInStatisticDto);
                response = new XbbResponse<>(signInStatisticVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 签到列表
     *
     * @param signInDTO 签到列表入参
     * @return SignInStatisticsVO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInList(@RequestBody @Valid SignInDTO signInDTO, BindingResult br) {
        XbbResponse<SignInListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.SIGNIN.getCode(), null, signInDTO.getDistributorMark());
                SignInListVO signInListVO = signInService.list(signInDTO);
                response = new XbbResponse<>(signInListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 有客户签到列表
     *
     * @param signInDTO 签到列表入参
     * @return SignInStatisticsVO
     */
    @RequestMapping(value = "/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInCustomer(@RequestBody @Valid SignInDTO signInDTO, BindingResult br) {
        XbbResponse<SignInListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.SIGNIN.getCode(), null, signInDTO.getDistributorMark());
                SignInListVO signInListVO = signInService.signInCustomerList(signInDTO);
                response = new XbbResponse<>(signInListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 签到轨迹
     *
     * @param signInTrackDTO 签到轨迹入参
     * @return SignInStatisticsVO
     */
    @RequestMapping(value = "/track", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInTrack(@RequestBody @Valid SignInTrackDTO signInTrackDTO, BindingResult br) {
        XbbResponse<SignInListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.SIGNIN.getCode(), null, signInTrackDTO.getDistributorMark());
                SignInListVO signInListVO = signInService.track(signInTrackDTO);
                response = new XbbResponse<>(signInListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 签到保存
     *
     * @param signInSaveDTO 签到保存入参
     * @return BaseVO
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInSave(@RequestBody @Valid SignInSaveDTO signInSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // ----------------------------------repeat post----------------------------------------------
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(signInSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(signInSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                // ----------------------------------repeat post----------------------------------------------
                BaseVO baseVO = signInService.save(signInSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 签到支持删除
     *
     * @param signInDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid SignInDeleteDTO signInDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = signInService.delete(signInDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 签到详情
     *
     * @param signInDetailDTO 签到详情入参
     * @return BaseVO
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String signInDetail(@RequestBody @Valid SignInDetailDTO signInDetailDTO, BindingResult br) {
        XbbResponse<SignInDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SignInDetailVO signInDetailVO = signInService.detail(signInDetailDTO);
                response = new XbbResponse<>(signInDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
